<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * Class School
 * @package App\Models
 */
class School extends Model
{
    const STATUS_ACTIVE = 1;
    const STATUS_DISABLE = 0;

    protected $fillable = [
        'name',
        'detail_address',
        'contact_name',
        'contact_phone',
        'dev_code',
        'objectid',
        'key',
        'key_id',
        'status',
        'province',
        'city',
        'country',
        'address'
    ];

    /**
     * @param $query
     * @param $name
     * @return mixed
     */
    public function scopeNameLike($query, $name)
    {
        return $query->when($name, function ($query) use ($name) {
            $query->where('name', 'like', '%' . $name . '%');
        });
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function rule()
    {
        return $this->hasOne(SchoolRule::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function activeAdmins()
    {
        return $this->hasMany(SchoolAdmin::class)->where('status', 1);
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeIsActive($query)
    {
        return $query->where("status",self::STATUS_ACTIVE);
    }
}
